package com.fan.play.media

import android.content.Context
import android.view.Surface
import com.fan.play.media.engine.CaptureDevice
import com.fan.play.media.engine.ICaptureDeviceListener
import com.fan.play.media.engine.MediaEngine

/**
 * 媒体引擎的代理类
 */
object MediaEngineAgent {

    private val mMediaEngine = MediaEngine()

    //硬件编码、解码
    private var mSoftEncode = false
    private var mSoftDecode = false

    fun create(context: Context): Boolean {
        return mMediaEngine.Create(context)
    }

    fun destroy() {
        mMediaEngine.Destroy()
    }

    /**
     * 设置编码、解码类型
     * @param softDecode 解码类型
     * @param softEncode 编码类型
     */
    fun setCodecType(softDecode: Boolean, softEncode: Boolean) {
        mSoftDecode = softDecode
        mSoftEncode = softEncode
        mMediaEngine.SetCodecType(softDecode, softEncode)
    }


    /**
     * 添加屏幕采集设备
     * @param width 屏幕宽
     * @param height 屏幕高
     * @return true:成功
     */
    fun addScreenDevice(width: Int, height: Int): Boolean {
        return mMediaEngine.AddScreenDevice(width, height)
    }


    /**
     * 添加网络采集设备
     * @param streamUrl 流地址
     * @param softDecode 解码模式
     * @param devType 设备类型
     * @return true:成功
     */
    fun addIPCameraDevice(streamUrl: String, softDecode: Boolean, devType: Int): Boolean {
        return mMediaEngine.AddIPCameraDevice(streamUrl, softDecode, devType)
    }

    /**
     * 删除设备
     * @param devID 设备ID
     */
    fun deleteCaptureDevice(devID: Int) {
        mMediaEngine.DeleteCaptureDevice(devID)
    }

    /**
     * 清空所有采集设备
     */
    fun clearCaptureDevice() {
        mMediaEngine.ClearCaptureDevice()
    }

    /**
     * 修改相机设备参数
     * @param width 相机预览宽
     * @param height 相机预览高
     */
    fun modifyCameraDevice(width: Int, height: Int) {
        mMediaEngine.ModifyCameraDevice(width, height)
    }

    /**
     * 修改相机设备参数
     * @param devID 设备ID
     * @param width 相机预览宽
     * @param height 相机预览高
     */
    fun modifyCameraDevice(devID: Int, width: Int, height: Int) {
        mMediaEngine.ModifyCameraDevice(devID, width, height)
    }

    /**
     * 修改音频参数
     * @param sampleRate 采样率
     * @param channelNum 采样通道数
     * @param samplesBitPer 采样点位数
     */
    fun modifyAudioDevice(sampleRate: Int, channelNum: Int, samplesBitPer: Int) {
        return mMediaEngine.ModifyAudioDevice(sampleRate, channelNum, samplesBitPer)
    }

    /**
     * 查找视频设备
     * @param devID 设备ID
     * @return 视频设备
     */
    fun findCaptureDevice(devID: Int): CaptureDevice? {
        return mMediaEngine.FindCaptureDevice(devID)
    }

    /**
     * 获取所有的视频采集设备
     * @return 视频采集设备集
     */
    fun getCaptureDevice(): ArrayList<CaptureDevice> {
        return mMediaEngine.GetCaptureDevice()
    }

    /**
     * 返回本地相机的数量
     * @return 数量
     */
    fun getCameraDeviceNum(): Int {
        return mMediaEngine.GetCameraDeviceNum()
    }

    fun registerCaptureDeviceListener(listener: ICaptureDeviceListener?) {
        mMediaEngine.RegisterCaptureDeviceListener(listener)
    }

    fun startCapture(devID: Int): Boolean {
        return mMediaEngine.StartCapture(devID)
    }

    fun stopCapture(devID: Int) {
        mMediaEngine.StopCapture(devID)
    }

    fun startPreview(devID: Int, surface: Surface) {
        mMediaEngine.StartPreview(devID, surface)
    }

    fun stopPreview(devID: Int) {
        mMediaEngine.StopPreview(devID)
    }

    fun startRecord(devID: Int, savePath: String) {
        mMediaEngine.StartRecord(devID, savePath)
    }

    fun stopRecord(devID: Int) {
        mMediaEngine.StopRecord(devID)
    }

    fun resumeRecord(devID: Int) {
        mMediaEngine.ResumeRecord(devID)
    }

    fun pauseRecord(devID: Int) {
        mMediaEngine.PauseRecord(devID)
    }

    fun startPush(devID: Int, pushUrl: String) {
        mMediaEngine.StartPush(devID, pushUrl)
    }

    fun stopPush(devID: Int) {
        mMediaEngine.StopPush(devID)
    }
}